var bcrypt = require('../utils/encrypt')
var db = require('../config/db')
var jwt = require('../utils/jwt')

//添加用户
exports.addUser = function (name, email, pwd, res) {
    let password = bcrypt.encryption(pwd);
    db.serialize(function () {
        var stmt = db.prepare("INSERT INTO user(name,pwd,email,register) VALUES (?,?,?,?)")
        stmt.run(name, password, email, new Date(), function (sta, reco) {
            if (sta == null) {
                res.send({ success: true })
            } else {
                res.send({ success: false, msg: reco })
            }
        })
        stmt.finalize();
    })
}

//匹配用户名和密码
exports.userMatch = function (data, pwd, res) {
    //data 手机号或邮箱
    db.serialize(function () {
        db.all("SELECT * FROM user WHERE email = ? OR phone = ?", data, function (err, rows) {
            if (err != null) {
                console.log(err)
                res.send({ success: false, msg: err })
            } else {
                if (rows.length > 0) {
                    if (bcrypt.verification(pwd, rows[0].pwd)) {
                        let token = jwt.genToken(rows[0].id, res)
                        rows[0].pwd = null
                        res.send({ success: true, token, info: rows[0] })
                    } else {
                        res.send({ success: false, msg: "用户名或密码错误" })
                    }
                } else {
                    res.send({ success: false, msg: "用户名或密码错误" })
                }
            }
        })
    })
}

//消息写入数据库
exports.addUserMsg = function (userId, peerId, content, state) {
    db.serialize(function () {
        var stmt = db.prepare("INSERT INTO message(user_id,friend_id,message,types,ctime,state) VALUES (?,?,?,?,?,?)")
        stmt.run(userId, peerId, content, 4, new Date(), state, function (sta, reco) {
            // if (sta == null) {
            //     res.send({ success: true })
            // } else {
            //     res.send({ success: false, msg: reco })
            // }
        })
        stmt.finalize();
    })
}


//获取用户的好友信息
exports.getFriendsInfo = function (id, res) {

    return new Promise(async (resolve, reject) => {
        sql = "select f.friend_id as groupId,f.nickname,u.name,u.email,u.phone,u.info,u.imgurl from friend f left join user u on f.friend_id = u.id where f.user_id = ?"
        db.all(sql, id, function (err, rows) {
            if (err != null) {
                console.log(err)
                res.send({ success: false, msg: err })
            } else {
                resolve(rows)
            }
        })
    })
}

//获取用户发给好友的信息或好友发给我的信息
exports.getFriendsMsg = function (id, res) {

    return new Promise(async (resolve, reject) => {
        sql = "select * from message m where (m.friend_id in (select f.friend_id from friend f where f.user_id = ?) and m.user_id=?) or (m.friend_id = ? and m.user_id in (select f.friend_id from friend f where f.user_id = ?))"
        db.all(sql, id, id, id, id, function (err, rows) {
            if (err != null) {
                console.log(err)
                res.send({ success: false, msg: err })
            } else {
                resolve(rows)
            }
        })
    })
}